package sqlanalysisgui.view.outline;

import org.apache.log4j.Logger;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import sqlanalysisgui.util.GUIUtils;
import sqlanalysisgui.view.model.LineInfo;
import sqlanalysisgui.view.model.TableInfo;
import sqlanalysisgui.view.model.TableListCrudInfoLineInfoModel;
import sqlanalysisgui.view.model.provider.TableListContentProvider;
import sqlanalysisgui.view.model.provider.TableListLabelProvider;

/*
 * http://www.vogella.com/articles/EclipseJFaceTree/article.html 사이트 참고 
 */
public class SqlCrudOutLine extends ViewPart {
	private Logger logger =  Logger.getLogger(this.getClass());
	public static final String ID = "sqlanalysisgui.view.outline.SqlCrudOutLine"; //$NON-NLS-1$
	private TreeViewer viewer;
	
	public SqlCrudOutLine() {
	}

	/**
	 * Create contents of the view part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		////////////////////////////////////////////
		viewer = new TreeViewer(container, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		Tree tree = viewer.getTree();
		viewer.setContentProvider(new TableListContentProvider());
		viewer.setLabelProvider(new TableListLabelProvider());
		// Expand the tree
		viewer.setAutoExpandLevel(3);
		// Provide the input to the ContentProvider
		//viewer.setInput(new TableListCrudInfoLineInfoModel());
		//setDataRefresh();
		viewer.getTree().setLinesVisible(true);
		// Add a doubleclicklistener

		hookClickEventHandler();
		////////////////////////////////////////////
		createActions();
		initializeToolBar();
		initializeMenu();
	}

	public void setDataRefresh(TableInfo tableInfo){
		viewer.setInput(new TableListCrudInfoLineInfoModel(tableInfo));
	}
	
	private void hookClickEventHandler(){
		viewer.addDoubleClickListener(new IDoubleClickListener() {

			@Override
			public void doubleClick(DoubleClickEvent event) {
				TreeViewer viewer = (TreeViewer) event.getViewer();
				IStructuredSelection thisSelection = (IStructuredSelection) event
						.getSelection();
				Object selectedNode = thisSelection.getFirstElement();
				viewer.setExpandedState(selectedNode,
						!viewer.getExpandedState(selectedNode));
				
				if(thisSelection.getFirstElement() instanceof LineInfo){
					//System.out.println(((LineInfo)selectedNode).getLine());
					IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
					
					GUIUtils.goToLine(activeEditor, ((LineInfo)selectedNode).getLineInteger());
				}
			}
		});

		viewer.getTree().addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(final KeyEvent e) {
				if (e.keyCode == SWT.DEL) {
					final IStructuredSelection selection = (IStructuredSelection) viewer
							.getSelection();
					if (selection.getFirstElement() instanceof LineInfo) {
						LineInfo o = (LineInfo) selection.getFirstElement();
						// TODO Delete the selected element from the model
						//System.out.println();
					}

				}
			}
		});
/*		tree.addSelectionListener(new SelectionAdapter() {
			  @Override
			  public void widgetSelected(SelectionEvent e) {
			    TreeItem item = (TreeItem) e.item;
			      if (item.getItemCount() > 0) {
			        item.setExpanded(!item.getExpanded());
			        // update the viewer
			        treeViewer.refresh();
			      }
			    }
			}); */
		
	}
	
	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}
}
